import {
    reqLogin, reqUserInfo
} from "@/api/user"
import { removeItem, setItem } from "@/utils/cookieUtil";
// state：仓库存储数据的地方
const state = {
    userInfo: {},
    roleList: []
}
// mutations：修改state的唯一手段，因此第一个参数必须是state，第二个参数为将要修改的数据
const mutations = {
    SET_USERINFO(state, res) {
        state.userInfo = res
    },
    LOGOUT(state, res) {
        removeItem("token")
        state.userInfo = {}
        state.roleList = []
    }

}
// action：书写业务逻辑（method方法）
const actions = {
    login({ commit }, userInfo) {
        return new Promise(async (resolve, reject) => {
            await reqLogin(userInfo).then(response => {
                setItem("Authorization", "Bearer " + response.access_token)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    getUserInfo({ commit }) {
        return new Promise(async (resolve, reject) => {
            await reqUserInfo().then(response => {
                console.log(response)
                commit("SET_USERINFO", response.data)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },


}
// getters：计算属性
const getters = {

}

export default {
    state,
    mutations,
    actions,
    getters
}